from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models

# Create your models here.


class Sight(models.Model):
    """景点"""
    name = models.CharField('景点名称', max_length=64)
    address = models.CharField('景点地址', max_length=64)
    comments = GenericRelation('Comment', related_query_name='sight_comments')


class Order(models.Model):
    """订单"""
    sn = models.CharField('订单号', max_length=64)
    amount = models.FloatField('订单金额')
    comments = GenericRelation('Comment', related_query_name='order_comments')  # 反向关联订单对应的评论


class Comment(models.Model):
    """评论"""
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')  # 评论关联的对象是订单还是景点

    content = models.CharField('评论内容', max_length=512)
    score = models.FloatField('分数', default=5)


